<template>
  <div class="page">
    <!-- 顶部栏 -->
    <van-nav-bar fixed placeholder :border="false">
      <template #title>
        <div class="fz-16 fw-bold">黄金</div>
      </template>
    </van-nav-bar>

    <div class="container">
      <!-- 用户信息 -->
      <div class="mb-30">
        <div class="flex mb-5">
          <div class="box" style="width: calc(100% / 3)">
            <div class="mb-5">总持有(克)</div>
            <div class="price hide-1">
              {{ userInfo.holdingGram || '0.00' }}
            </div>
          </div>

          <div class="box ml-5 mr-5" style="width: calc(100% / 3)">
            <div class="mb-5">累计持有</div>
            <div class="price hide-1">
              {{ userInfo.accumulatedHoldingGram || '0.00' }}
            </div>
          </div>

          <div class="box" style="width: calc(100% / 3)">
            <div class="mb-5">总价值</div>
            <div class="price hide-1">
              {{ userInfo.totalValue || '0.00' }}
            </div>
          </div>
        </div>

        <div class="box flex-1 mb-5">
          <div class="mb-5">累计收益</div>
          <div class="price">
            {{ userInfo.accumulatedHoldingProfit || '0.00' }}
          </div>
        </div>

        <div class="box flex flex-rb">
          <div>
            <span>昨日收益：</span>
            <span class="price price--sm" v-if="userInfo.yesterdayProfit >= 0">
              +{{ userInfo.yesterdayProfit || '0.00' }}
            </span>
            <span
              class="price price--sm price--error"
              v-else-if="userInfo.yesterdayProfit < 0"
            >
              {{ userInfo.yesterdayProfit || '0.00' }}
            </span>
          </div>

          <div>
            <span>持有收益：</span>
            <span class="price price--sm" v-if="userInfo.holdingProfit >= 0">
              +{{ userInfo.holdingProfit || '0.00' }}
            </span>
            <span
              class="price price--sm price--error"
              v-else-if="userInfo.holdingProfit < 0"
            >
              {{ userInfo.holdingProfit || '0.00' }}
            </span>
          </div>
        </div>
      </div>

      <!-- 实时金价 -->
      <gold-comp class="mb-30" @refresh="initPage"></gold-comp>

      <!-- 待生效记录 -->
      <div>
        <div class="flex flex-cc flex-rb">
          <div class="price price--sm">
            <span>待生效：</span>
            <span>{{ userInfo.pendingEffectiveGram || '0.00' }}</span>
          </div>

          <div class="click cl-white" @click="router.push('/order/trade')">
            买入卖出明细
          </div>
        </div>

        <ul class="cl-white" v-if="orders.length">
          <li class="box mt-10" v-for="(v, i) in orders" :key="i">
            <div v-if="v.type === 1">
              买入{{ v.tradeAmount }}U，预计{{ v.effectiveTime }}确认份额
            </div>
            <div v-else>
              卖出{{ v.tradeAmount }}克，预计{{ v.effectiveTime }}确认生效
            </div>
          </li>
        </ul>

        <!-- 暂无数据 -->
        <van-empty v-else>
          <span class="cl-white">暂无数据</span>
        </van-empty>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, provide } from 'vue'
import goldApi from 'js/gold.api'

import goldComp from './components/gold.vue'

const router = useRouter()
const route = useRoute()

// 用户信息
const userInfo = ref({})
provide(
  'userInfo',
  computed(() => userInfo.value)
)

const orders = ref([])

// 获取用户Id
if (route.query.userId) {
  sessionStorage.setItem('userId', route.query.userId)
}

initPage()

function initPage() {
  getUserInfo()
  getGoldOrderList()
}

// 获取用户信息
async function getUserInfo() {
  const params = {
    financialProductId: 1,
  }

  const { code, data } = await goldApi.getUserInfo(params)

  if (code === 200) {
    userInfo.value = data
  }
}

// 获取理财产品订单列表
async function getGoldOrderList() {
  const params = {
    financialProductId: 1,
    status: 1,
    type: 1,
    current: 1,
    size: 100000,
  }

  const { code, data } = await goldApi.getGoldOrderList(params)

  if (code === 200) {
    orders.value = data.records || []
  }
}
</script>

<style lang="scss" scoped></style>
